package com.wisdomstar.web.controller.wisdomstar;

import com.github.pagehelper.PageInfo;
import com.wisdomstar.common.core.domain.AjaxResult;
import com.wisdomstar.common.exception.base.BaseException;
import com.wisdomstar.ws.domain.Category;
import com.wisdomstar.ws.domain.vo.CategoryVO;
import com.wisdomstar.ws.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author RinMi
 * @since 2023/6/6
 */

@RestController
@RequestMapping("ws/category")
@Tag(name = "ws-category-controller", description = "文档类别信息管理接口")
public class WsCategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "分页查询文档类别信息", notes = "分页查询文档类别信息", httpMethod = "GET")
    @GetMapping("listCategory")
    public AjaxResult listCategory(@ApiParam("页码") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @ApiParam("页大小") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                   @ApiParam("父节点") @RequestParam(value = "parentId", required = false) Long parentId,
                                   @ApiParam("类别名称") @RequestParam(value = "categoryName", required = false) String categoryName) {
        try {
            Category category = new Category();
            category.setParentId(parentId);
            category.setCategoryName(categoryName);
            PageInfo<Category> categoryPageInfo = categoryService.listCategory(category, pageNum, pageSize);
            return AjaxResult.success(categoryPageInfo);
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }

    @ApiOperation(value = "新增文档类别信息", notes = "新增文档类别信息", httpMethod = "POST")
    @PostMapping("insertCategory")
    public AjaxResult insertCategory(@ApiParam("文档类别信息") @RequestBody @Validated CategoryVO categoryVO) {
        try {
            Category category = new Category();
            BeanUtils.copyProperties(categoryVO, category);
            int count = categoryService.insertCategory(category);
            if (count == 1) {
                return AjaxResult.success("新增成功");
            } else {
                return AjaxResult.error("新增失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    @ApiOperation(value = "修改文档类别信息", notes = "新增文档类别信息", httpMethod = "POST")
    @PostMapping("updateCategory")
    public AjaxResult updateCategory(@ApiParam("文档类别信息") @RequestBody CategoryVO categoryVO) {
        try {
            Category category = new Category();
            BeanUtils.copyProperties(categoryVO, category);
            int count = categoryService.updateCategory(category);
            if (count == 1) {
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation(value = "删除文档类别信息", notes = "删除文档类别信息", httpMethod = "DELETE")
    @DeleteMapping("removeCategory")
    public AjaxResult removeCategory(@ApiParam("文档类别id") @RequestParam Long categoryId) {
        try {
            int count = categoryService.removeCategory(categoryId);
            if (count == 1) {
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败");
            }
        } catch (BaseException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }
}
